home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-21 | 4.1 KB | 178 lines | [TEXT/R*ch] |
- /*
- Helpers_ut.cp
- © 1996 Bob Boylan
-
- Revision History
- MacHack 1996 initial creation
- */
- #include "Helpers_ut.h"
- #include "UAppleEventsMgr.h"
- #include "AEPrint.h"
- #include <algo.h>
- #include <string.h>
-
- // -----------------------------------------------------------------
- // As4CharString
- //
- string
- As4CharString( Int_32 the4Chars )
- {
- union Mess
- {
- char theCharType[8];
- DescType theDescType;
- } Mess;
-
- Mess.theDescType = the4Chars;
- Mess.theCharType[4] = '\0';
-
- return string( Mess.theCharType );
- }
-
-
- // -------------------------------------------------------------
- // AsInt32 - a direct descendant of powerplant extractor (ty MW)
- //
- Int_32
- AsInt32 ( const AEDesc &inDesc )
- {
-
- Handle dataH;
- AEDesc coerceDesc = {typeNull, nil};
-
- if (inDesc.descriptorType == typeLongInteger) {
- dataH = inDesc.dataHandle; // Descriptor is the type we want
-
- } else { // Try to coerce to the desired type
- if (AECoerceDesc(&inDesc, typeLongInteger, &coerceDesc) == noErr) {
- // Coercion succeeded
- dataH = coerceDesc.dataHandle;
-
- } else { // Coercion failed
- throw (long) errAETypeError;
- }
- }
-
- Int_32 theRetVal = **(Int_32**) dataH; // Extract value from Handle
- if (coerceDesc.dataHandle != nil) {
- AEDisposeDesc(&coerceDesc);
- }
- return theRetVal;
- }
-
- // -------------------------------------------------------------
- // AsBoolean - a direct descendant of powerplant extractor (ty MW)
- //
- Boolean
- AsBoolean ( const AEDesc &inDesc )
- {
-
- Handle dataH;
- AEDesc coerceDesc = {typeNull, nil};
-
- if (inDesc.descriptorType == typeBoolean) {
- dataH = inDesc.dataHandle; // Descriptor is the type we want
-
- } else { // Try to coerce to the desired type
- if (AECoerceDesc(&inDesc, typeBoolean, &coerceDesc) == noErr) {
- // Coercion succeeded
- dataH = coerceDesc.dataHandle;
-
- } else { // Coercion failed
- throw (long) errAETypeError;
- }
- }
-
- Boolean theRetVal = **(Boolean**) dataH; // Extract value from Handle
- if (coerceDesc.dataHandle != nil) {
- AEDisposeDesc(&coerceDesc);
- }
- return theRetVal;
- }
-
- // -----------------------------------------------------------------
- // ListAsstring ... cute little routine to handle ae coercions a little better than gizmos
- //
- string
- ListAsstring( const AEDesc &inDesc );
- string
- ListAsstring( const AEDesc &inDesc )
- {
- string theRetVal("[ ");
- OSErr theErr;
- Int_32 theNItems = 0;
- theErr = AECountItems(&inDesc, &theNItems);
-
- for( Int_32 theIndex = 1 ; theIndex <= theNItems ; theIndex++ )
- {
- {
- StAEDescriptor theNthItemD;
- DescType theBogusK;
- theErr = AEGetNthDesc(&inDesc, theIndex, typeWildCard, &theBogusK, &theNthItemD.mDesc);
- if( theNthItemD.mDesc.descriptorType == typeAEList )
- {
- theRetVal += ListAsstring( theNthItemD.mDesc );
- }
- else
- {
- theRetVal += Asstring( theNthItemD.mDesc );
- }
- }
-
- if( theIndex != theNItems )
- {
- theRetVal += ",";
- }
-
-
- }
-
- theRetVal += " ]";
-
- return theRetVal;
- }
-
- // -----------------------------------------------------------------
- // Asstring
- //
- string
- Asstring ( const AEDesc &inDesc )
- {
- // quick exit check
- if( inDesc.descriptorType == typeNull ) return string("''");
- if( inDesc.descriptorType == typeAEList ) return ListAsstring( inDesc );
-
- string theRetVal;
- Handle dataH;
- AEDesc coerceDesc = {typeNull, nil};
-
- if (inDesc.descriptorType == typeChar) {
- dataH = inDesc.dataHandle; // Descriptor is the type we want
-
- } else { // Try to coerce to the desired type
- if (AECoerceDesc(&inDesc, typeChar, &coerceDesc) == noErr) {
- // Coercion succeeded
- dataH = coerceDesc.dataHandle;
-
- } else { // Coercion failed ... use gizmos
- const UInt_32 theBUFFSIZE = 150;
- char thePrintBuff[theBUFFSIZE];
- AEDesc *theNonConstP = (AEDesc *) &inDesc;
- OSErr theErr = AEPrint( theNonConstP, thePrintBuff, theBUFFSIZE-1 );
- theRetVal = string( thePrintBuff, min( theBUFFSIZE,strlen(thePrintBuff) ) );
- }
- }
-
- if( theRetVal.length() == 0 )
- {
- Int_32 strLength = GetHandleSize(dataH);
- if (strLength > 255) strLength = 255;
- theRetVal = string( *dataH, strLength );
- }
-
- if (coerceDesc.dataHandle != nil) {
- AEDisposeDesc(&coerceDesc);
- }
- return theRetVal;
- }
-